<?php
namespace Common\Controller;
use Common\Controller\BaseController;

class AdminController extends BaseController{

	protected function _initialize(){

		parent::_initialize();
		if(!session('adminInfo.id')){
			session('[destroy]');
			$this->redirect('Admin/Public/index');
		}
    /*$authCheck = true;
		if($authCheck){
			//增加默认权限控制动作
			$noCheck = array('Index/index', 'Index/main');
			if(!$this->authCheck(CONTROLLER_NAME . '/' . ACTION_NAME, $noCheck)){
				if(IS_AJAX){
					$this->ajaxReturn(array('info' => '没有权限！', 'status' => '0'));
				}else{
					exit('没有权限');
				}
			}
		}*/
		//$menu = session('left_menu');
		if(!$menu){
			$menuList = $this->getList('id,pid,title,name', 'node', 'status=1 and id in (' . session('adminInfo.rules') . ')', 'sort desc,id asc',false,null);
			foreach($menuList as $v){
				if($v['pid'] == 0){
					foreach($menuList as $value){
						$value['pid'] == $v['id'] && $v['sub'][] = $value;
					}
					$menu[] = $v;
				}
			}
			session('left_menu', $menu);
		}
		$this->assign('menuList', $menu);
	}

	protected function sortNode($data, &$result, $pid = 0, $num = 1){
		foreach($data as $key => $value){
			if($value['pid'] == $pid){
				$value['num'] = $num;
				$result[] = $value;
				$this->sortNode($data, $result, $value['id'], $num + 1);
			}
		}
	}

	public function setStatus(){
		$status = I('get.status', 0, 'intval');
		$result = M(CONTROLLER_NAME)->where('id=' . I('get.id', 0, 'intval'))->setField('status', ($status == 1 ? 0 : 1));
		$this->returnResult($result);
	}

	public function setSort(){
		$sort = I('get.sort', 100, 'intval');
		$result = M(CONTROLLER_NAME)->where('id=' . I('get.id', 0, 'intval'))->setField('sort', $sort);
		$this->returnResult($result);
	}

	private function authCheck($name, $noCheck = array()){
		$aid = session('adminInfo.id');
		if(empty($name) || $aid <= 0){
			return false;
		}elseif($aid == 1 || in_array($name, $noCheck)){
			return true;
		}else{
			$rules = M()->table('app_group g,app_admins a')->where('a.gid=g.id and a.id=' . $aid)->getField('g.rules');
			if($rules){
				$rulesArr = explode(',', $rules);
				$rulesId = M('node')->where(array('status' => 1, 'name' => $name))->getField('id');
				return in_array($rulesId, $rulesArr);
			}
			return false;
		}
	}
}
?>